#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_PHOTO_AUTO			(0)
#define ITEM_PHOTO_ZOOM_IN		(1)
#define ITEM_PHOTO_ZOOM_OUT		(2)
#define ITEM_PHOTO_ROTATE			(3)
#define ITEM_PHOTO_EXIT			(4)

TRect		MediaPhotoTouchRect[IMG_MEDIA_PHOTO_ITEM_QTY];
TPosCoor	MediaPhotoItemPos[IMG_MEDIA_PHOTO_ITEM_QTY] = { {8,376}, {146,376}, {292,376}, {434,376},{576,376} };
TPosCoor MediaPhotoButBkg = {0,365};
int		MediaPhotoFocus = -1;
TFrame		MediaPhotoFrame;
char		MediaPhotoInfoStr[40];
extern	unsigned char read_info_data[10];
static int 		exit_flag = 0;//0:exit is valid,1:exit is invalid.
extern unsigned char navi_run;
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
static  int AutoPlay_count;
char FileName[20];
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern 		int 	Last_ID ;


void MediaPhotoDrawInfo( TCanvas *pCanvas, int idx)
{
	TRect rect; 

if (idx == ITEM_PHOTO_AUTO )/////////1220 add
	{
		g_FillRect( pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
		DrawJpeg(pCanvas, 0, 0, "/1.jpg" );	
		return;
	}
		
	g_FillRect( pCanvas, 260, 200, 460, 250, RGB_WHITE );
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );
	//if (idx == ITEM_PHOTO_AUTO )/////////1220 add
	//	return;
	
	switch(idx)
	{
		case ITEM_PHOTO_AUTO:	
			sprintf(MediaPhotoInfoStr, "AUTO");		
			break;
		case ITEM_PHOTO_ZOOM_IN:
			sprintf(MediaPhotoInfoStr, "ZOOM IN");		
			break;
		case ITEM_PHOTO_ZOOM_OUT:			
			sprintf(MediaPhotoInfoStr, "ZOOM OUT");		
			break;
		case ITEM_PHOTO_ROTATE:
			sprintf(MediaPhotoInfoStr, "ROTA");		
			break;		
	}
	rect.left = 260;
	rect.top = 200;
	rect.right = 460;	
	rect.bottom = 250;	
	g_Puts( pCanvas, FONT33, rect, TEXT_ALIGN_CENTER, MediaPhotoInfoStr );
}

void MediaPhotoDrawBackgroup(TCanvas *pCanvas)
{	
	char FileName[20];
	TPosCoor pos;
	
	g_FillRect( pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
	DrawJpeg(pCanvas, 0, 0, "/1.jpg" );				
	pos = MediaPhotoButBkg;
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_PIC_FOLDER, IMG_MEDIA_PHOTO_BUTBKG_COVER);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);	
}

void MediaPhotoDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				MediaPhotoInit();
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);		
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MediaPhotoDrawVolume(MediaPhotoFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}


void MediaPhotoDrawJPEG(TCanvas *pCanvas)
{	
	g_FillRect( pCanvas, 0, 0, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, RGB_BLACK );
	DrawJpeg(pCanvas, 0, 0, FileName );	
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
	
}

void MediaPhotoDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;
	if( idx < 0 || idx >= IMG_MEDIA_PHOTO_ITEM_QTY )
		return;


	if (idx == ITEM_PHOTO_EXIT)
		{			
			if ( !navi_run)//navi_run :0 close  1: active
				exit_flag = 1;
			else
				exit_flag = 0;
			g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_BLACK, 0, 0 );
				if (exit_flag)//invalid
					{
						GetJpegIdx = IMG_MEDIA_PHOTO_EXIT_VALID ;											
					}
					else
					{
						if( bfocus)
							GetJpegIdx = IMG_MEDIA_PHOTO_ITEM_FOCUS_EXIT ;
						else
							GetJpegIdx =IMG_MEDIA_PHOTO_ITEM_EXIT ;				
					}				
			pos = MediaPhotoItemPos[idx];
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_PIC_FOLDER, GetJpegIdx);	
	
			pJpeg = GetJpegPtr(FileName);
			DrawJpeg(pCanvas, pos.x, pos.y, FileName);
			MediaPhotoTouchRect[idx].left = pos.x;
			MediaPhotoTouchRect[idx].right = pos.x + pJpeg.Width;
			MediaPhotoTouchRect[idx].top = pos.y;
			MediaPhotoTouchRect[idx].bottom = pos.y + pJpeg.Height;
			/*if (exit_flag)//invalid
				{			
					g_Puts( pCanvas, FONT33, MediaPhotoTouchRect[idx], TEXT_ALIGN_CENTER, "EXIT");
				}
				*/
		}
	else
		{
		
			if( bfocus )
				GetJpegIdx = IMG_MEDIA_PHOTO_ITEM_FOCUS_AUTO+idx ;
			else
				GetJpegIdx = IMG_MEDIA_PHOTO_ITEM_AUTO+idx ;
			pos = MediaPhotoItemPos[idx];
			sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_PIC_FOLDER, GetJpegIdx);	
	
			pJpeg = GetJpegPtr(FileName);
			DrawJpeg(pCanvas, pos.x, pos.y, FileName);
			MediaPhotoTouchRect[idx].left = pos.x;
			MediaPhotoTouchRect[idx].right = pos.x + pJpeg.Width;
			MediaPhotoTouchRect[idx].top = pos.y;
			MediaPhotoTouchRect[idx].bottom = pos.y + pJpeg.Height;
		}
	
}



void MediaPhotoDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		MediaPhotoDrawBackgroup(MediaPhotoFrame.pCanvas);
	for( i = 0; i < IMG_MEDIA_PHOTO_ITEM_QTY; i++ )
		MediaPhotoDrawItem( MediaPhotoFrame.pCanvas, i, FALSE );
	if (mute_status == 1)
		{
			MediaPhotoDrawVolume(MediaPhotoFrame.pCanvas,1);
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}

int MediaPhotoGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MEDIA_PHOTO_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MediaPhotoTouchRect[i] ) )
			return i;
	return -1;
}


BOOL MediaPhotoBtnEnter(int idx)
{
//	printf ("in MediaPhotoBtnEnter idx is %d\n",idx);
	unsigned char MediaExit_data [5] = {0xe0,0x03,0x05,0x02,0xea};
	MediaPhotoDrawItem( MediaPhotoFrame.pCanvas, idx, FALSE );
	switch(idx)
	{
		case ITEM_PHOTO_AUTO:
			MediaPhotoDrawInfo( MediaPhotoFrame.pCanvas, idx );
			AutoPlay_count = 1;
			dtpresent_flag++;		
			pthread_cond_signal(&dt_cond);
			//dt_Present();
			//dt_Present_part(MediaPhotoTouchRect [idx]);
			break;
		case ITEM_PHOTO_ZOOM_IN:
			MediaPhotoDrawInfo( MediaPhotoFrame.pCanvas, idx );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaPhotoTouchRect [idx]);
			break;
		case ITEM_PHOTO_ZOOM_OUT:
			MediaPhotoDrawInfo( MediaPhotoFrame.pCanvas, idx );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaPhotoTouchRect [idx]);
			break;
		case ITEM_PHOTO_ROTATE:
			MediaPhotoDrawInfo( MediaPhotoFrame.pCanvas, idx );
			//dtpresent_flag++;		
			//pthread_cond_signal(&dt_cond);
			//dt_Present();
			dt_Present_part(MediaPhotoTouchRect [idx]);
			break;
		case ITEM_PHOTO_EXIT:
			if (dev_SPIWrite(MediaExit_data,5)!=0)
			printf ("write spi data error!\n");
			MenuMediaInit();
	}
	LOG( "btn = %d\n", idx );	

	return TRUE;
}

void JPEGPlay(char next_pre)//1 is next ,0 is prev.
{
	static int JpegIdx=1;	
	if (next_pre)
		{
			JpegIdx = (JpegIdx>=10) ? 1 : (++JpegIdx);	
	 	}
	 else
	 	{
	 		JpegIdx = (JpegIdx<=1) ? 10 : (--JpegIdx);	
	 	}
	 		sprintf( FileName, "/%d.jpg", JpegIdx);
	 		MediaPhotoDrawJPEG(MediaPhotoFrame.pCanvas);
//	 		printf ("JpegIdx is %d,",JpegIdx);
	
}

void MediaPhotoInit()
{
	ir_SetCurrentID( IR_MEDIA_PHOTO_INIT );
	
	frm_Create( &MediaPhotoFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
	MediaPhotoDraw( TRUE );
	AutoPlay_count = 0;

}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	

	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
						///////////////for test,because this main board send info error. becarefull!
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:
									//		MenuMediaInit();/////////////////test 20071204
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}

									rc = 0;//if handled, clear this flag.	
								read_info_data[0] = 0;			
																									
						break;
						
					case	ID_KEY_STATUS:
						//it is identify the exit button.	
						
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;						
						break;
						
					case	ID_SCREEN_INFO:								
								if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MediaPhotoDrawVolume(MediaPhotoFrame.pCanvas,1);
								}	
								
								read_info_data[0] = 0;	
								break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:
										MediaPhotoDrawVolume(MediaPhotoFrame.pCanvas,1);
										break;
									case	ID_DMUTE:
										MediaPhotoDrawVolume(MediaPhotoFrame.pCanvas,3);
										break;	
									case 	ID_FMAM:
										MenuRadioInit();//band command;
										break;	
									case 	ID_SEEKUP:
										JPEGPlay (1);
										break;
									case 	ID_SEEKDN:
										JPEGPlay (0);								
										break;
									case 	ID_DEST:
									case 	ID_NAVI:
										//lastID = IR_MEDIA_MUSIC_INIT;	
										MenuNaviInit();
										break;
									
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;						 
						break;
						
					case 	ID_BAND_SEL:
						MenuRadioInit();
						rc = 0;//if handled, clear this flag.	
						read_info_data[0] = 0;			
						break;		
						
					default:	
							rc = -1;						
						}
		return 0;				
}

void MediaPhotoInput(UInt32 key)
{
	int flag;
	static int JpegIdx = 1;	
	
	 if (AutoPlay_count > 0)
	 	{
	 		AutoPlay_count ++;
	 		if (AutoPlay_count >400)
	 			{
	 				JpegIdx ++;
	 				AutoPlay_count = 1;
	 			}
	 		if (JpegIdx >400)
	 			{
	 				AutoPlay_count = 1;
	 				JpegIdx = 1;
	 				//printf (" AutoPlay_count >400!\n");
	 				JPEGPlay (1);	 				
	 			}
	 	}
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
			if (flag == 0)
				{
					//printf ("main menu handle receive data success!\n");
					return;
				}			
		}
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( MediaPhotoFocus = MediaPhotoGetTouchID( pos )) != -1 )
				{
					MediaPhotoDrawItem( MediaPhotoFrame.pCanvas, MediaPhotoFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					//dt_Present();
					dt_Present_part(MediaPhotoTouchRect [MediaPhotoFocus]);
				}
			}
		}
		else
		{
			if( ( focus = MediaPhotoGetTouchID( pos ) ) == MediaPhotoFocus )
					#ifdef INVALID_KEY
					if ((AutoPlay_count == 0 ) && ((MediaPhotoFocus != ITEM_PHOTO_EXIT) ||	\
						 ((MediaPhotoFocus == ITEM_PHOTO_EXIT)&&(!exit_flag))) ) //exit valid
					#endif
					{
							MediaPhotoBtnEnter( MediaPhotoFocus );
					}
					else
						{
							MediaPhotoDraw( TRUE );
							AutoPlay_count = 0;
							dtpresent_flag++;		
							pthread_cond_signal(&dt_cond);
							//dt_Present();
									
						}
			else if( MediaPhotoFocus != -1 )
			{
				//MediaPhotoDrawItem( MediaPhotoFrame.pCanvas, MediaPhotoFocus, FALSE );
				MediaPhotoDraw( TRUE );
				AutoPlay_count = 0;	
				dtpresent_flag++;		
				pthread_cond_signal(&dt_cond);			
				//dt_Present();
			}
			MediaPhotoFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

